



def f(x,n):
    if x<=1: return x
    return ((n-3)*f(x-1,n) + (n-2)*f(x-2,n))%n


# print(f(100,100)) 

# print(f(2019,511)) 
# print(f(1334,2047))
# print(f(2021,1023))
print(f(10,10))
print(f(50,10))
print(f(10,50))